package com.neutech.Demo1;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLOutput;
import java.sql.Statement;

public class Demo1 {
    public static void main(String[] args) {
        Connection connection=null;
        // 1.加载驱动类
        //利用反射加载驱动类
        try {
            // mysql 8.x版本需要带com.mysql.cj.jdbc.Driver 8.x以下版本不用带x
            Class.forName("com.mysql.cj.jdbc.Driver");

            //2.获取连接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/s_t","root","root");

        //使用连接对象，获取一个sql的执行对象
//            Statement statement = connection.createStatement();

            String sql = "select * from student where Sname= ? and Sno= ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            //ResultSet 结果集 将查询出来的所有sql数据存放在resultSet中
            ResultSet resultSet = preparedStatement.executeQuery();

            //next() 向下移动指针并且返回boole类型值表达是否有数据
            while (resultSet.next()){
                System.out.print(resultSet.getString("sno"));
                System.out.print(resultSet.getString("sname"));
                System.out.print(resultSet.getString("ssex"));
                System.out.print(resultSet.getInt("sage"));
                System.out.println(resultSet.getString("dept"));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
